package com.example.demo.controller;

import com.example.demo.entity.ArticleEntity;
import com.example.demo.service.ArticleService;
import com.example.demo.vo.ArticleListVO;
import com.example.demo.vo.common.PageVO;
import com.example.demo.vo.common.PageableVO;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.stream.Collectors;

/**
 * 博客首页
 */
@Controller
public class BlogIndexController {
    private final ArticleService articleService;

    public BlogIndexController(ArticleService articleService) {
        this.articleService = articleService;
    }

    /**
     * 博客首页
     *
     * @param pageable
     * @return
     */
    @GetMapping("/")
    public ModelAndView blogList(
            // 分页参数
            PageableVO pageable
    ) {
        // 该页面默认一页显示3行，覆盖默认配置20行
        pageable.setSize(3);
        // 读取Entity集合
        Page<ArticleEntity> entityPage = articleService.findPage(pageable);
        // 将 ArticleEntity 集合转换为 ArticleListVO 集合，裁减掉不需要展示的数据，并转换日期格式
        // 转换后的结果封闭为 PageVO， 附加分页信息
        PageVO<ArticleListVO> voPage = PageVO.convert(entityPage, entityPage.getResult().stream().map(ArticleListVO::convert).collect(Collectors.toList()));
        return new ModelAndView("client/index", "page", voPage);
    }
}
